package SeedBank

import org.springframework.dao.DataIntegrityViolationException

class MailController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
		
		if(session?.user){
            def userMail = User.get(session.user.id).messages

            if(userMail){
                params.max = Math.min(params.max ? params.int('max') : 10, 100)
                render(view: 'list' , model: [userMail: userMail] ) 
            }else{
                flash.message = "No messages!"
                redirect(controller: "mail", action: "create")
            }
			
		}else{
			flash.message = "Login before creating viewing messages!"
			redirect(controller: "User", action: "login")
		}

    }

    def create() {
        [mailInstance: new Mail(params)]
    }

    def save() {
		def toUser = User.findByLogin(params.toUser)
		if(toUser){
			params.fromUser = session.user.login
			params.toId = toUser.id
			params.fromId = session.user.id
			def mailInstance = new Mail(params)
			mailInstance.user = toUser;
			toUser.messages.add(mailInstance);
			
			if (!mailInstance.save(flush: true)) {
				render(view: "create", model: [mailInstance: mailInstance])
				return
			}
			
			flash.message = message(code: 'default.created.message', args: [message(code: 'mail.label', default: 'Mail'), mailInstance.id])
			redirect(action: "show", id: mailInstance.id)
		}else{
			flash.message = "Couldn't find user!"
			redirect(controller: "Mail", action: "create")
		}
		
    }

    def show() {
        def mailInstance = Mail.get(params.id)
        if (!mailInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'mail.label', default: 'Mail'), params.id])
            redirect(action: "list")
            return
        }

        [mailInstance: mailInstance]
    }

    def edit() {
        def mailInstance = Mail.get(params.id)
        if (!mailInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'mail.label', default: 'Mail'), params.id])
            redirect(action: "list")
            return
        }

        [mailInstance: mailInstance]
    }

    def update() {
        def mailInstance = Mail.get(params.id)
        if (!mailInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'mail.label', default: 'Mail'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (mailInstance.version > version) {
                mailInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'mail.label', default: 'Mail')] as Object[],
                          "Another user has updated this Mail while you were editing")
                render(view: "edit", model: [mailInstance: mailInstance])
                return
            }
        }

        mailInstance.properties = params

        if (!mailInstance.save(flush: true)) {
            render(view: "edit", model: [mailInstance: mailInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'mail.label', default: 'Mail'), mailInstance.id])
        redirect(action: "show", id: mailInstance.id)
    }

    def delete() {
        def mailInstance = Mail.get(params.id)
        if (!mailInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'mail.label', default: 'Mail'), params.id])
            redirect(action: "list")
            return
        }

        try {
            mailInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'mail.label', default: 'Mail'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'mail.label', default: 'Mail'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
